﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GeekShop
{
    public partial class GradientColorDialog : Form
    {
        #region Fields
        private ColorBar colorBar;
        private Gradient defaultGradient = Gradient.TransparentFade;
        private MarkerOptions markerOptions;
        #endregion

        #region Static Fields
        private static GradientColorDialog form = null;
        #endregion

        #region Properties
        public Gradient Gradient
        {
            get { return this.colorBar.Gradient; }
            set { this.colorBar.Gradient = value; }
        }
        #endregion

        #region Static Properties
        public static GradientColorDialog Form
        {
            get
            {
                if (GradientColorDialog.form == null)
                {
                    GradientColorDialog.form = new GradientColorDialog(Gradient.TransparentFade);
                }
                return GradientColorDialog.form;
            }
            set
            {
                if (GradientColorDialog.form == null)
                {
                    GradientColorDialog.form = value;
                }
            }
        }
        #endregion

        #region Constructors
        public GradientColorDialog(Gradient gradient)
        {
            InitializeComponent();
            this.colorBar = new ColorBar(gradient);
            this.colorBar.Location = new Point(3, 3);
            this.colorBar.Name = "colorBar";
            this.colorBar.Size = new Size(this.Width - 12, 40);
            this.Controls.Add(this.colorBar);
            this.markerOptions = new MarkerOptions();
            this.markerOptions.Location = new Point(3, this.colorBar.Location.Y + this.colorBar.Size.Height);
            this.markerOptions.Name = "markerOptions";
            this.markerOptions.Size = new Size(this.Width - 12, 45);
            this.markerOptions.TabIndex = 0;
            this.Controls.Add(this.markerOptions);

            this.colorBar.SelectionChanged += new MarkerSelectionChangedHandler(colorBar1_SelectionChanged);
            this.markerOptions.ColorChanged += new MarkerColorChangedHandler(markerOptions1_ColorChanged);
            this.markerOptions.MarkerDeleted += new MarkerDeletedHandler(markerOptions1_MarkerDeleted);
            this.markerOptions.MarkerMoved += new MarkerMovedHandler(markerOptions_MarkerMoved);
        }
        public GradientColorDialog(): this(Gradient.TransparentFade) { }
        #endregion

        #region Public Methods
        public void Reset()
        {
            if (this.colorBar != null)
            {
                this.colorBar.Gradient = this.defaultGradient;
            }
        }
        public DialogResult RunDialog()
        {
            return GradientColorDialog.Form.ShowDialog();
        }
        #endregion

        #region Event Methods
        void markerOptions1_MarkerDeleted(object sender, MarkerEventArgs m)
        {
            this.colorBar.tryRemoveMarker(m);
        }
        void markerOptions1_ColorChanged(object sender, MarkerEventArgs m)
        {
            this.colorBar.trySetColor(m);
        }
        void markerOptions_MarkerMoved(object sender, MarkerEventArgs m)
        {
            this.colorBar.MoveMarker(m);
        }
        void colorBar1_SelectionChanged(object sender, MarkerEventArgs m)
        {
            this.markerOptions.Index = m.NewIndex;
            this.markerOptions.Marker = m.NewMarker;
        }
        private void btOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        #endregion
    }
}
